package com.seven.z7.logviewer;

import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;

import javax.swing.ImageIcon;

/**
 * 
 * @author aprokofjev
 * 
 */
class TabCloseUI implements MouseListener, MouseMotionListener {

	private ClosableTabbedPane mTabbedPane;
	private int mCloseX;
	private int mCloseY;
	private int mMeX;
	private int mMeY;
	private int mSelectedTab;
	private int mIconWidth;
	private int mIcontHeight;
	private Rectangle mRect;
	private ImageIcon mCloseIcon;

	public TabCloseUI(ClosableTabbedPane pane) {
		URL url = getClass().getResource("/img/close_tab.png");
		mCloseIcon = new ImageIcon(url);
		mIconWidth = mCloseIcon.getIconWidth();
		mIcontHeight = mCloseIcon.getIconHeight();
		mRect = new Rectangle(0, 0, mIconWidth, mIcontHeight);
		mTabbedPane = pane;
		mTabbedPane.addMouseMotionListener(this);
		mTabbedPane.addMouseListener(this);
	}

	public void mouseEntered(MouseEvent me) {
	}

	public void mouseExited(MouseEvent me) {
		mMeX = me.getX();
		mMeY = me.getY();
		if (mouseOverTab(me.getX(), me.getY())) {
			controlCursor();
			mTabbedPane.repaint();
		}
	}

	public void mousePressed(MouseEvent me) {
	}

	public void mouseClicked(MouseEvent me) {
	}

	public void mouseDragged(MouseEvent me) {
	}

	public void mouseReleased(MouseEvent me) {
		if (closeUnderMouse(me.getX(), me.getY())) {
			boolean isToCloseTab = mTabbedPane.tabAboutToClose(mSelectedTab);
			if (isToCloseTab && mSelectedTab > -1) {
				mTabbedPane.removeTabAt(mSelectedTab);
			}
			mSelectedTab = mTabbedPane.getSelectedIndex();
		}
	}

	public void mouseMoved(MouseEvent me) {
		mMeX = me.getX();
		mMeY = me.getY();

		if (mouseOverTab(mMeX, mMeY)) {
			controlCursor();
			mTabbedPane.repaint();
		} else if (mTabbedPane.getCursor().getType() != Cursor.DEFAULT_CURSOR) {
			mTabbedPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		}
	}

	private void controlCursor() {
		if (mTabbedPane.getTabCount() > 0) {
			if (closeUnderMouse(mMeX, mMeY)) {
				mTabbedPane.setCursor(new Cursor(Cursor.HAND_CURSOR));
				if (mSelectedTab > -1) {
					mTabbedPane.setToolTipTextAt(mSelectedTab, "Close");
				}
			} else {
				mTabbedPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
				if (mSelectedTab > -1) {
					mTabbedPane.setToolTipTextAt(mSelectedTab, "");
				}
			}
		}
	}

	private boolean closeUnderMouse(int x, int y) {
		mRect.x = mCloseX;
		mRect.y = mCloseY;
		return mRect.contains(x, y);
	}

	public void paint(Graphics g) {
		for (int j = 0; j < mTabbedPane.getTabCount(); j++) {
			int x = mTabbedPane.getBoundsAt(j).x + mTabbedPane.getBoundsAt(j).width - mIconWidth;
			int y = mTabbedPane.getBoundsAt(j).y;
			drawClose(g, x, y, j);
		}

	}

	private void drawClose(Graphics g, int x, int y, int j) {
		if (mTabbedPane != null && mTabbedPane.getTabCount() > 0) {
			int offset = (mTabbedPane.getBoundsAt(j).height - mCloseIcon.getIconHeight()) / 2;
			System.out.println("j: " + offset);
			g.drawImage(mCloseIcon.getImage(), x, y + offset, null);
		}
	}

	private boolean mouseOverTab(int x, int y) {
		for (int j = 0; j < mTabbedPane.getTabCount(); j++) {
			if (mTabbedPane.getBoundsAt(j).contains(x, y)) {
				mSelectedTab = j;
				mCloseX = mTabbedPane.getBoundsAt(j).x + mTabbedPane.getBoundsAt(j).width - mIconWidth;
				mCloseY = mTabbedPane.getBoundsAt(j).y;
				return true;
			}
		}
		return false;
	}

}
